//
//  HNSH_ChatListCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/18.
//

#import "HNSH_ChatListCell.h"

@interface HNSH_ChatListCell ()
/**  */
@property (nonatomic, strong)UIImageView * imgView;
/**  */
@property (nonatomic, strong)UILabel * countLabel;
/**  */
@property (nonatomic, strong)UILabel * nameLabel;
/** */
@property (nonatomic, strong)UILabel * levelLabel;
/**  */
@property (nonatomic, strong)UILabel * conLabel;
/**  */
@property (nonatomic, strong)UILabel * timeLabel;
/**  */
@property (nonatomic, strong)UIView * segView;

/* messageIcon */
@property (nonatomic, strong) UIImageView *messageIcon;
@end

@implementation HNSH_ChatListCell

- (void)layoutSubviews {
    [super layoutSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.selectedBackgroundView.backgroundColor = [UIColor appWhiteLabelColor];
}

#pragma mark - Cycle Methods
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor appWhiteLabelColor];
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

//MARK: Myself Methods
- (void)addAllViews{
    [self.contentView addSubview:self.imgView];
    [self.contentView addSubview:self.countLabel];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.levelLabel];
    [self.contentView addSubview:self.conLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.segView];
    [self.contentView addSubview:self.messageIcon];
}

- (void)addAllLayout{
    [self.imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.width.height.mas_equalTo(48);
        make.centerY.mas_equalTo(self.contentView);
    }];
    [self.countLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.imgView.mas_right);
        make.centerY.mas_equalTo(self.imgView.mas_top);
        make.width.height.mas_equalTo(18);
    }];
    [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgView.mas_right).offset(10);
        make.top.mas_equalTo(self.imgView.mas_top).offset(4);
        make.height.mas_equalTo(20);
    }];
    [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(6);
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.width.mas_equalTo(26);
        make.height.mas_equalTo(17);
    }];
    [self.conLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgView.mas_right).offset(10);
        make.right.mas_equalTo(-48);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(4);
        make.height.mas_equalTo(17);
    }];
    [self.timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(100);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(17);
        make.height.mas_equalTo(14);
    }];
    [self.segView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(-12);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.messageIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.timeLabel);
        make.bottom.mas_equalTo(-15);
        make.width.height.mas_equalTo(20);
    }];
}

//MARK: Setter Methods
- (void)setDataModel:(RCConversationModel *)model{
    [super setDataModel:model];

    RCMessageContent * lastest = model.lastestMessage;
    if ([lastest isKindOfClass:[RCGroupNotificationMessage class]]) {
        RCGroupNotificationMessage * no = (RCGroupNotificationMessage *)lastest;
        self.conLabel.text = [NSString stringWithFormat:@"%@%@",model.hasUnreadMentioned ? @"[有人@我]" : @"",no.message];
        self.conLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:self.conLabel.text SelectContent:@"[有人@我]" CommonFont:self.conLabel.font SpeFont:self.conLabel.font Color:[UIColor colorWithHexString:@"#FF3E35"]];;
    } else {
        self.conLabel.text = [NSString stringWithFormat:@"%@%@",model.hasUnreadMentioned ? @"[有人@我]" : @"",lastest.conversationDigest];
        self.conLabel.attributedText = [ST_AttributeLabelTool handleLabelWithContent:self.conLabel.text SelectContent:@"[有人@我]" CommonFont:self.conLabel.font SpeFont:self.conLabel.font Color:[UIColor colorWithHexString:@"#FF3E35"]];;
    }
    self.timeLabel.text = [RCKitUtility convertMessageTime:model.sentTime / 1000];
    if (model.unreadMessageCount) {
        self.countLabel.hidden = NO;
        NSString *str = [NSString stringWithFormat:@"%ld",model.unreadMessageCount];
        CGFloat width = [str widthForFont:self.countLabel.font];
        [self.countLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width+10);
        }];
        self.countLabel.text = str;
    } else {
        self.countLabel.hidden = YES;
    }
    //消息免打扰 model.blockStatus == 0
    self.messageIcon.hidden = model.blockStatus != 0;
    if (model.blockStatus == 0) {//消息免打扰
        self.countLabel.hidden = YES;
    }
    
    if (model.conversationType == ConversationType_GROUP) {
        RCGroup * group = [[RCIM sharedRCIM] getGroupInfoCache:model.targetId];
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:group.portraitUri] placeholderImage:[UIImage imageNamed:@"chat_icon_myqun"]];
        self.levelLabel.hidden = YES;
        self.nameLabel.text = group.groupName;
    } else {
        RCUserInfo * userInfo = [[RCIM sharedRCIM] getUserInfoCache:model.targetId];
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:userInfo.portraitUri] placeholderImage:[UIImage imageNamed:@"base_user_header_icon"]];
        self.nameLabel.text = userInfo.name;
        NSDictionary *dict = [userInfo.extra jsonValueDecoded];
//        NSLog(@"extend ==> %@",dict);
        self.levelLabel.hidden = NO;
        self.levelLabel.text = [NSString stringWithFormat:@"V%ld",[dict[@"level"] integerValue]];
    }
}

//MARK: Getter Methods
- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.layer.cornerRadius = 10;
        _imgView.layer.masksToBounds = YES;
    }
    return _imgView;
}

- (UIImageView *)messageIcon{
    if (!_messageIcon) {
        _messageIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"chat_message_dis"]];
    }
    return _messageIcon;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [UILabel new];
        _countLabel.font = [UIFont systemFontOfSize:13];
        _countLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        _countLabel.backgroundColor = [UIColor colorWithHexString:@"#FF3E35"];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.layer.cornerRadius = 9;
        _countLabel.layer.masksToBounds = YES;
    }
    return _countLabel;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont boldSystemFontOfSize:14];
        _nameLabel.textColor = [UIColor appBlack333LabelColor];
    }
    return _nameLabel;
}

- (UILabel *)levelLabel{
    if (!_levelLabel) {
        _levelLabel = [UILabel new];
        _levelLabel.font = [UIFont systemFontOfSize:12];
        _levelLabel.textColor = [UIColor appWhiteLabelColor];
        _levelLabel.textAlignment = NSTextAlignmentCenter;
        _levelLabel.backgroundColor = [UIColor colorWithHexString:@"#FB6F1C"];
        _levelLabel.layer.cornerRadius = 4.0f;
        _levelLabel.layer.masksToBounds = YES;
    }
    return _levelLabel;
}

- (UILabel *)conLabel{
    if (!_conLabel) {
        _conLabel = [UILabel new];
        _conLabel.font = [UIFont systemFontOfSize:12];
        _conLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    }
    return _conLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont systemFontOfSize:10];
        _timeLabel.textColor = [UIColor colorWithHexString:@"#8E8E93"];
        _timeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _timeLabel;
}

- (UIView *)segView{
    if (!_segView) {
        _segView = [UIView new];
        _segView.backgroundColor = [UIColor colorWithHexString:@"#EFEFF4"];
    }
    return _segView;
}

@end
